package openjdk.foreach;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Asin Liu on 2017/2/21.
 */
public class ForEach {
    public static void main(String[] args) {

        //new ForEach().jdk8ToLoopList();
        new ForEach().jdk8ToLoopListedMap();
    }

    private void normalWayToLoopList() {

        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");
        for (String obj : list) {
            System.out.println(obj);
        }
    }

    private void jdk8ToLoopList() {
        List<String> lst = new ArrayList<>();
        lst.add("A");
        lst.add("B");
        lst.add("C");
        lst.add("D");
        lst.add("E");
        lst.add("F");

        //lst.forEach(item -> System.out.println(item));
        System.out.println("======================");
        lst.forEach(item -> {
            if ("C".equals(item)) {
                System.out.println(item);
            }
        });

        System.out.println("======================");

        lst.forEach(System.out::println);

        System.out.println("======================");
        lst.stream().filter(s -> s.contains("B")).forEach(System.out::println);
    }


    /**
     *
     */
    private void normalWayToLoopMap() {
        // Normal way to loop a map
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);
        map.put("D", 40);
        map.put("E", 50);
        map.put("F", 60);
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("Item" + entry.getKey() + " Count" + entry.getValue());
        }
    }

    private void jdk8ToLoopMap() {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 10);
        map.put("B", 20);
        map.put("C", 30);
        map.put("D", 40);
        map.put("E", 50);
        map.put("F", 60);

        map.forEach((k, v) -> System.out.println("Item:" + k + "  Count:" + v));

        System.out.println("-----------------------------");

        map.forEach((k, v) -> {
            System.out.println("Item:" + k + " Count:" + v);
            if ("E".equals(k)) {
                System.out.println("Hello E");
            }
        });
    }


    private void jdk8ToLoopListedMap() {
        List<Map<String, String>> lst = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        Map<String, String> map = new HashMap<>();
        map.put("a1", "a1x");
        map.put("a2", "a2x");
        lst.add(map);

        Map<String, String> mp = new HashMap<>();
        mp.put("b1", "b1x");
        mp.put("b2", "b2x");
        lst.add(mp);

        lst.stream().forEach(e -> {
            System.out.println(e.getClass());
            sb.append("\r\n-----------------------\r\n");
            e.keySet().stream().forEach((m) -> sb.append(m + ":" + e.get(m) + " "));
            System.out.println(sb);
        });
    }
}
